<template>
  <div class="app-container">
    <public-table>
      <template slot="table">

        <el-table fit border v-if="refreshTable" v-loading="loading" :data="deptList" row-key="id"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <el-table-column prop="label" label="名称"></el-table-column>
          <el-table-column prop="type" label="类型" width="150" align="center">
            <template slot-scope="scope">
              <el-tag size="medium" v-if="scope.row.type === 1" type="danger">岗位</el-tag>
              <el-tag size="medium" v-else>部门</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="handleUpdate(scope.row)" v-if="scope.row.type === 1">修改
              </el-button>
              <el-button size="small" type="text" @click="handleAdd(scope.row)"
                v-if="scope.row.type === 0 || scope.row.level === 1">添加岗位
              </el-button>
              <el-button size="small" type="text" @click="handleDelete(scope.row)" v-if="scope.row.type === 1">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </public-table>

    <el-dialog :visible.sync="viewVisible" append-to-body width="40%" title="基本信息">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-card class="box-card" style="margin-top: 20px">-->
        <!--          <div style="width: 150px; height: 10px; background-color: var(--primary-color);position:relative;top:-15px"></div>-->
        <!--          <div class="title20Class">-->
        <!--            <svg-icon icon-class="info" class="svgClass"/>-->
        <!--            基本信息-->
        <!--          </div>-->
        <!--          <el-divider content-position="left"></el-divider>-->
        <el-row>
          <el-col :span="20">
            <el-form-item label="岗位名称" prop="postName">
              <el-input size="small" v-model="form.postName" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!--        </el-card>-->
        <div class="dialog-footer" style="text-align: center; margin-top: 10px">
          <el-button size="small" @click="cancel">取 消</el-button>
          <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { deptPostTree } from '@/api/system/dept'
import { addPost, delPost, updatePost } from '@/api/system/post'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Dept',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      viewVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        postName: '',
        deptId: ''
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true
      deptPostTree(this.queryParams).then(response => {
        this.deptList = response.data
        this.loading = false
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      console.log('---------node--------', node)
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      }
    },
    // 取消按钮
    cancel() {
      this.viewVisible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        postName: '',
        deptId: ''
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.viewVisible = true
      this.form = {
        postName: '',
        deptId: row.id,
        superiorsId: row.superiorsId,
        type: 'add'
      }
      // this.$router.push({ name: 'PostAdd', params: row })
    },
    handleExport() {
      this.$modal.msgError('开发中')
    },
    /**
     * 历史版本
     */
    handleHis() {
      this.$router.push({
        path: '/menu12/dept/deptInfoHis'
      })
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.viewVisible = true
      this.form = {
        postName: row.label,
        postId: row.id,
        superiorsId: row.superiorsId,
        type: 'update'
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.id
      this.$modal.confirm('是否确认删除岗位的数据？').then(function () {
        return delPost(postIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form)
      if (this.form.type === 'add') {
        this.$refs['form'].validate(valid => {
          if (valid) {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.viewVisible = false
              this.getList()
            })
          }
        })
      } else {
        this.$refs['form'].validate(valid => {
          if (valid) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.viewVisible = false
              this.getList()
            })
          }
        })
      }
    }
  }
}
</script>
